
package com.seekting.study;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class TestUrlConnection {
    private static String URL = "http://cml.ksmobile.com/WallPaper/getWallPaperList?w=1080&h=1920";

    public static void main(String[] args) {
        InputStream inputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;

        try {
            URL = URL + "&l_id_t=2&l_id=1773&p_n=3";
            URL url = new URL(URL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            int responseCode = connection.getResponseCode();

            if (responseCode == 200) {
                inputStream = connection.getInputStream();
                byteArrayOutputStream = new ByteArrayOutputStream();
                byte[] data = new byte[1024];
                int length = 0;
                while ((length = inputStream.read(data)) != -1) {
                    byteArrayOutputStream.write(data, 0, length);
                }
                String s = byteArrayOutputStream.toString();
                System.out.println(s);
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (byteArrayOutputStream != null) {
                try {
                    byteArrayOutputStream.close();
                } catch (IOException e) {
                }
                byteArrayOutputStream = null;
            }
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                }
                inputStream = null;
            }

        }
    }
}
